package com.ruoyi.web.controller.system;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.exp.DeliveryExport;
import com.ruoyi.system.domain.exp.SendExport;
import com.ruoyi.system.service.impl.NotificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Delivery;
import com.ruoyi.system.service.IDeliveryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@RestController
@RequestMapping("/system/delivery")
public class DeliveryController extends BaseController {
    @Resource
    private IDeliveryService deliveryService;

    private final NotificationService notificationService;

    public DeliveryController(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    /**
     * 查询采购单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:list')")
    @GetMapping("/list")
    public TableDataInfo list(Delivery delivery) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getDeptId().equals(1000001L)) {
            delivery.setDeptId(loginUser.getDeptId());
        }
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        return getDataTable(list);
    }

    /**
     * 根据日期查询配送单
     */
    @GetMapping("/selectDeliveryDay")
    @Anonymous
    public AjaxResult selectDeliveryDay(Delivery delivery) {
        return AjaxResult.success(deliveryService.selectDeliveryDay(delivery));
    }

    /**
     * 导出采购单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Delivery delivery) {
        List<DeliveryExport> list = deliveryService.selectDeliveryList_forExport(delivery);
        ExcelUtil<DeliveryExport> util = new ExcelUtil<DeliveryExport>(DeliveryExport.class);
        util.exportExcel(response, list, "采购单数据");
    }

    /**
     * 导出配送列表
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:export')")
    @Log(title = "配送单", businessType = BusinessType.EXPORT)
    @PostMapping("/send_export")
    public void send_export(HttpServletResponse response, Delivery delivery) {
        List<SendExport> list = deliveryService.selectDeliveryList_forSendExport(delivery);
        ExcelUtil<SendExport> util = new ExcelUtil<SendExport>(SendExport.class);
        util.exportExcel(response, list, "配送单数据");
    }

    /**
     * 获取采购单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(deliveryService.selectDeliveryById(id));
    }

    /**
     * 获取采购单详细信息
     */
//    //@PreAuthorize("@ss.hasPermi('system:delivery:query')")
    @GetMapping(value = "/anonymous/{id}")
    @Anonymous
    public AjaxResult getInfo_anonymous(@PathVariable("id") Long id) {
        return success(deliveryService.selectDeliveryById(id));
    }

    /**
     * 新增采购单
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:add')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Delivery delivery) {
        return toAjax(deliveryService.insertDelivery(delivery));
    }

    /**
     * 修改采购单
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:edit')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Delivery delivery) {
        return toAjax(deliveryService.updateDelivery(delivery));
    }

    /**
     * 删除采购单
     */
    //@PreAuthorize("@ss.hasPermi('system:delivery:remove')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deliveryService.deleteDeliveryByIds(ids));
    }

    @PostMapping("/competeSuccess/{logId}")
    @Anonymous
    public AjaxResult competeSuccess(@PathVariable Long logId) {
        deliveryService.competeSuccess(logId);
        return AjaxResult.success();
    }

    @PostMapping("/testSchedule")
    @Anonymous
    public AjaxResult testSchedule(String time, Long logId) {
        LocalDateTime notificationTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        notificationService.scheduleNotification(notificationTime, logId);
        return AjaxResult.success();
    }
}
